//
//  BDAllBusinessViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/11.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BDAllBusinessViewController.h"
#import "BDBusinessListCell.h"
#import "BDGetBusinessListRequest.h"
#import "BDBusinessListModel.h"
#import "UIViewController+BDAddBusinessHelper.h"

static NSString * const cellReuseIdentifier = @"cell";



@interface BDAllBusinessViewController ()

@property(nonatomic,strong)NSIndexPath *curselectIndex; //当前选择的cell
@property(nonatomic,strong)BDBusinessListModel* modelList;


@end

@implementation BDAllBusinessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib:[UINib nibWithNibName:@"BDBusinessListCell" bundle:nil] forCellReuseIdentifier:cellReuseIdentifier];
    
    [self hsy_headerBeginRefreshing]; //加载数据
    
    [self.titleButton setTitle:@"所有商户" forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)hsy_showEmptyImage{
    return YES;
}

- (void)hsy_loadServerData{
    [super hsy_loadServerData];
    
    if (self.pageIdex <= 0) {
        self.pageIdex = 1;
    }

    BDGetBusinessListRequest *task = [[BDGetBusinessListRequest alloc] init];
    task.auid = [BDUserAccount instatce].auid;
    task.pageIndex = self.pageIdex;
    task.sortType = 1;
    
    __weak typeof(self)weakSelf = self;

    [BDNetworkManager POST:task completionHander:^(id responseObject, NSString *resultString) {
        if (responseObject) {
            BDBusinessListModel *modelList = [BDBusinessListModel mj_objectWithKeyValues:responseObject];
            NSInteger totalPage = modelList.page.totalPage;

            if (weakSelf.pageIdex != 1) {
                [weakSelf.modelList.list addObjectsFromArray:modelList.list];
                modelList.list = weakSelf.modelList.list;
            }
            weakSelf.modelList = modelList;
            
            if (totalPage<= weakSelf.pageIdex) {
                [weakSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusNoMoreData];
            }else{
                [weakSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusDefault];
            }

        }else{
            [weakSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusFailed];
            if (resultString) {
                [weakSelf showMessage:resultString];
            }
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelList.list.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger height = 46.0;
    if (self.curselectIndex && self.curselectIndex.row == indexPath.row) {
        height = 114.0;
    }
    
    return height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BDBusinessModel *model = self.modelList.list[indexPath.row];
    
    BDBusinessListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier forIndexPath:indexPath];
    
    cell.shopNameLabel.text = model.shortName;
    cell.countTradeLabel.text = [NSString stringWithFormat:@"%ld笔",(long)model.countTrade];
    cell.totalTradeAmountLabel.text = [NSString stringWithFormat:@"%.2f元",model.totalTradeAmount];
    [cell.adressBtn setTitle:model.address forState:UIControlStateNormal];
    cell.adressDetailLabel.text = model.address;

    cell.callphoneBlock = ^{
        if (model.contactCellphone) {
            NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",model.contactCellphone];
            // NSLog(@"str======%@",str);
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    };
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.curselectIndex && self.curselectIndex.row == indexPath.row) {
        BDBusinessModel *model = self.modelList.list[indexPath.row];
        [self pushToShopInfoWithModel:model];
    }else{
        if (self.curselectIndex) {
            [self.tableView reloadRowsAtIndexPaths:@[self.curselectIndex] withRowAnimation:UITableViewRowAnimationFade];
        }
        self.curselectIndex = indexPath;
    }
    
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)pushToShopInfoWithModel:(BDBusinessModel *)model{
    if (!model)return;
    
    model = [BDBusinessModel mj_objectWithKeyValues:[model mj_keyValues]];
    model.auStep =@"-1";
    __weak typeof(self) wSelf = self;
    [wSelf nextStepWihBusinessModel:model userShopModel:nil readOnly:YES];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
